﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start;
            return input.slice(start);
        }
        return [];
    }
});
app.controller('MasterJobtitleRoleController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice) {

    $scope.MCategory = []
    $scope.MenuCategorise = function (data) {
        var index = $scope.MCategory.indexOf(data);
        if (index == -1) {
            $scope.MCategory.push(data);
        }
        else {
            $scope.MCategory.splice(index, 1);
        }

    }

    $scope.MainMenu = []
    $scope.MainMenuCategory = function (data) {
        var index = $scope.MainMenu.indexOf(data);
        if (index == -1) {
            $scope.MainMenu.push(data);
        }
        else {
            $scope.MainMenu.splice(index, 1);
        }
    }

    $scope.SubMenu = []
    $scope.SubMenuCategory = function (data) {
        var index = $scope.SubMenu.indexOf(data);
        if (index == -1) {
            $scope.SubMenu.push(data);
        }
        else {
            $scope.SubMenu.splice(index, 1);
        }
    }

    var menucategory = "";
    $scope.$watchCollection('MCategory', function (newCategory, oldCategory) {
        if (newCategory.length > 0) {
            menucategory = "";
            for (var i = 0; i <= newCategory.length-1; i++) {
                menucategory += ',' + newCategory[i].MenuCategoryId;
                console.log(menucategory);
            }
        }
    });

    var MainMenu = "";
    $scope.$watchCollection('MainMenu', function (newCategory, oldCategory) {
        if (newCategory.length > 0) {
            MainMenu = "";
            for (var i = 0; i <= newCategory.length - 1; i++) {
                MainMenu += ',' + newCategory[i].MainMenuID;
                console.log(menucategory);
            }
        }
    });


    var SubMenu = "";
    $scope.$watchCollection('SubMenu', function (newCategory, oldCategory) {
        if (newCategory.length > 0) {
            SubMenu = "";
            for (var i = 0; i <= newCategory.length - 1; i++) {
                SubMenu += ',' + newCategory[i].SubMenuID;
                console.log(menucategory);
            }
        }
    });



    url = "GetChurchBoards";
    ApplicationService.Getdata(url).success(function (data) {
        $scope.ChurchBoards = data;
    });

    $scope.$watch('role.BoardJobtitle.BoardId', function (nboardid, oBoardid) {
        
        if (angular.isUndefined(nboardid)==false) {
            url = "GetChurchBoardJobtitleByBoardId";
            ApplicationService.Getdata(url, nboardid).success(function (data) {
                $scope.ChurchJobtitle = data;
            });
        }
    });
    
    $scope.JobtitleRolestatus = [{ StatusId: 1, StatusName: "Active" }, { StatusId: -1, StatusName: "InActive" }]

    BindGrid();

    url = "GetMenus";
    ApplicationService.Getdata(url).success(function (data) {
        debugger;
        $scope.Menus = data;
        console.log(data);
    });
    

    $scope.toggle = function (modelstate, id) {
        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Jobtitle Role";
                $scope.role = null;
                menucategory = "";
                MainMenu = "";
                SubMenu = "";
                break;
            case 'edit':
                $scope.title = "Edit Jobtitle Role";
                url = "GetChurchJobtitleRole";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.id = id;
                    menucategory = data.UserType.MenuCategory;
                    MainMenu = data.UserType.MainMenu;
                    SubMenu = data.UserType.SubMenu;
                    $scope.role = data;
                });
                break;
        }
        $("#JobtitleRole").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        url = "GetChurchJobtitleRoles";
        ApplicationService.Getdata(url).success(function (data) {
            $scope.list = data;
            $scope.currentPage = 1;
            $scope.entryLimit = 10;
            $scope.filteredItems = $scope.list.length;
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "DeleteChurchJobtitleRole";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }

    var usertype = {
        UserTypeId: "",
        OrganizationTypeId: "",
        UserTypeName: "",
        MainMenu: menucategory,
        SubMenu: SubMenu,
        landingPage: "",
        MenuCategory: menucategory,
    }
    var BoardJobtitle = {
        BoardJobtitleId: "",
        BoardId: "",
        JobtitleName: ""
    }
    var JobtitleRole = {
        JobtitleRoleId: "",
        UserTypeId: "",
        BoardJobtitleId: "",
        status: "",
        UserType: usertype,
        JobtitleRoleId: "",
        BoardJobtitle: BoardJobtitle
    }

    $scope.save = function (modelstate) {
        JobtitleRole.LandingPage = $scope.role.LandingPage;
        JobtitleRole.UserType.SubMenu = SubMenu;
        JobtitleRole.UserType.MainMenu = MainMenu;
        JobtitleRole.UserType.MenuCategory = menucategory;
        JobtitleRole.status = $scope.role.status;
        JobtitleRole.BoardJobtitleId = $scope.role.BoardJobtitleId;
        
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "AddChurchJobtitleRoles";
                break;
            case 'edit':
                JobtitleRole.JobtitleRoleId = $scope.id;
                url = "UpdateChurchJobtitleRole";
                break;
        }

        ApplicationService.Save(JobtitleRole, url).success(function (result) {
            alertboxservice.ShowMsg(result, "JobtitleRole ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }


}]);

